package com.xli.metadata.frameconfig.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xli.dto.component.model.TableModel;
import com.xli.dto.controller.BaseController;
import com.xli.dto.params.IdsDTO;
import com.xli.dto.result.ResultVO;
import com.xli.dto.result.status.Status;
import com.xli.dto.validation.group.IGroup;
import com.xli.metadata.frameconfig.entity.FrameConfig;
import com.xli.metadata.frameconfig.entity.dto.ConfigAddDTO;
import com.xli.metadata.frameconfig.entity.dto.ConfigSearchDTO;
import com.xli.metadata.frameconfig.entity.dto.ConfigUpdateDTO;
import com.xli.metadata.frameconfig.entity.mapper.IFrameConfigMapper;
import com.xli.metadata.frameconfig.entity.vo.ConfigVO;
import com.xli.metadata.frameconfig.service.IFrameConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "系统参数")
@RestController
@RequestMapping("/frameConfig")
public class FrameConfigController extends BaseController {

    @Autowired
    private IFrameConfigService iFrameConfigService;

    @Operation(summary = "保存参数", description = "保存系统参数")
    @PostMapping(value = "/add")
    public ResultVO<String> add(@RequestBody @Validated(IGroup.add.class) ConfigAddDTO dto) {
        FrameConfig frameConfig = iFrameConfigService.findConfigByName(dto.getConfigName());
        if (frameConfig == null) {
            frameConfig = IFrameConfigMapper.INSTANCE.toEntity(dto);
            frameConfig.setId(IdUtil.getSnowflakeNextId());
            boolean result = iFrameConfigService.insert(frameConfig);
            if (result) {
                return new ResultVO<>(Status.SUCCESS, "保存成功");
            }
            return new ResultVO<>(Status.FAILED, "保存失败");
        }
        return new ResultVO<>(Status.FAILED, "保存失败，参数名已存在");
    }

    @Operation(summary = "删除参数", description = "删除系统参数")
    @PostMapping(value = "/delete")
    public ResultVO<String> delete(@RequestBody @Validated(IGroup.delete.class) IdsDTO dto) {
        int i = 0;
        for (String id : dto.getIds()) {
            if (iFrameConfigService.delete(Long.parseLong(id))) {
                i++;
            }
        }
        return new ResultVO<>(Status.SUCCESS, i + "条数据删除成功");
    }

    @Operation(summary = "更新参数", description = "更新系统参数")
    @PostMapping(value = "/update")
    public ResultVO<String> update(@RequestBody @Validated(IGroup.update.class) ConfigUpdateDTO dto) {
        FrameConfig frameConfig = iFrameConfigService.find(Long.valueOf(dto.getId()));
        if (frameConfig != null) {
            IFrameConfigMapper.INSTANCE.merge(frameConfig, IFrameConfigMapper.INSTANCE.toEntity(dto));
            if (iFrameConfigService.update(frameConfig)) {
                return new ResultVO<>(Status.SUCCESS, "更新成功");
            }
            return new ResultVO<>(Status.FAILED, "更新失败");
        }
        return new ResultVO<>(Status.FAILED, "更新失败，参数已存在");
    }

    @Operation(summary = "查询参数详情", description = "查询系统参数详情")
    @PostMapping(value = "/detail/{id}")
    public ResultVO<ConfigVO> detail(@PathVariable("id") @NotBlank(message = "id不能为空") String id) {
        FrameConfig frameConfig = iFrameConfigService.find(Long.valueOf(id));
        if (frameConfig != null) {
            return new ResultVO<>(Status.SUCCESS, "查询成功", IFrameConfigMapper.INSTANCE.toVO(frameConfig));
        }
        return new ResultVO<>(Status.SUCCESS, "查询失败");
    }

    @Operation(summary = "查询参数列表", description = "查询系统参数列表")
    @PostMapping(value = "/search")
    public ResultVO<TableModel<ConfigVO, FrameConfig, ConfigSearchDTO>> search(@RequestBody @Validated(IGroup.search.class) ConfigSearchDTO dto) {

        TableModel<ConfigVO, FrameConfig, ConfigSearchDTO> tableModel = new TableModel<>(dto) {
            @Override
            public List<ConfigVO> fetch(int current, int pageSize) {
                Page<FrameConfig> page = iFrameConfigService.findList(getQueryWrapper(), current, pageSize);
                this.setTotal(page.getTotal());
                return page.convert(IFrameConfigMapper.INSTANCE::toVO).getRecords();
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", tableModel);
    }
}
